home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3576 / 3576.xpi / chrome / googleshortcuts.jar / content / googlebuttonsOverlay.js next >
Text File  |  2009-12-13  |  14KB  |  372 lines

  1. if(!de) var de={};
  2. if(!de.soerenrinne) de.soerenrinne={};
  3. if(!de.soerenrinne.GoogleButtons) de.soerenrinne.GoogleButtons={};
  4.  
  5. de.soerenrinne.GoogleButtons = function(){
  6.     
  7.     var pub = {};
  8.     
  9.     googlebuttonsPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("de.soerenrinne.googlebuttons.");
  10.     
  11.     var servicesList = googlebuttonsPrefs.getCharPref('wholeshebang');
  12.     var soerenrinneServicesArray = new Array();
  13.     soerenrinneServicesArray = servicesList.split(',');
  14.     soerenrinneServicesArray.sort(function(x,y){ 
  15.         var a = String(x).toUpperCase(); 
  16.         var b = String(y).toUpperCase(); 
  17.         if (a > b) 
  18.             return 1 
  19.         if (a < b) 
  20.             return -1 
  21.         return 0; 
  22.     });
  23.     var soerenrinneNumberOfServices = soerenrinneServicesArray.length;
  24.     
  25.     PrefListener = function(branchName, func)
  26.     {
  27.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  28.         var branch = prefService.getBranch(branchName);
  29.         branch.QueryInterface(Components.interfaces.nsIPrefBranch2);
  30.     
  31.         this.register = function()
  32.         {
  33.             branch.addObserver("", this, false);
  34.             branch.getChildList("", { })
  35.                   .forEach(function (name) { func(branch, name); });
  36.         };
  37.     
  38.         this.unregister = function unregister()
  39.         {
  40.             if (branch)
  41.                 branch.removeObserver("", this);
  42.         };
  43.     
  44.         this.observe = function(subject, topic, data)
  45.         {
  46.             if (topic == "nsPref:changed")
  47.                 func(branch, data);
  48.         };
  49.     }
  50.     
  51.     GoogleButtons_renew = function() {
  52.         
  53.         try {
  54.             if (document.getElementById('GIcon') != null) {
  55.                 document.getElementById('GIcon').hidden = true;
  56.             }
  57.             
  58.             // Add toolbar items (if not added before)
  59.             if (document.getElementById("googlebuttonsOrientation") != null) {
  60.                 var box = document.getElementById("googlebuttonsOrientation"); // a <menupopup> element
  61.                 
  62.                 for (var j=0; j<soerenrinneNumberOfServices; j++) {
  63.                     var id = soerenrinneServicesArray[j];
  64.                     var idStripped = id.replace(/\ /g, "").toLowerCase();
  65.                     if (googlebuttonsPrefs.getBoolPref(id) == true) {
  66.                         var child = create_ToolbarButton(idStripped+"1", id, "false", "open_website(event, '" + id + "_url');");
  67.                     } else {
  68.                         var child = create_ToolbarButton(idStripped+"1", id, "true", "open_website(event, '" + id + "_url');");
  69.                     }
  70.                     if (document.getElementById(idStripped+"1") == null) {
  71.                         box.insertBefore(child, box.lastChild);
  72.                     }
  73.                 }
  74.             }
  75.                         
  76.             // Add menu items (if not added before)
  77.             if (document.getElementById("googlebuttons_menupopup") != null) {
  78.                 var menupopup = document.getElementById("googlebuttons_menupopup");
  79.                 
  80.                 for (var j=0; j<soerenrinneNumberOfServices; j++) {
  81.                     var id = soerenrinneServicesArray[j];
  82.                     var idStripped = id.replace(/\ /g, "").toLowerCase();
  83.                     if (googlebuttonsPrefs.getBoolPref(id) == true) {
  84.                         var child = create_MenuItem(idStripped, id, "false", "open_website(event, '" + id + "_url');");
  85.                     } else {
  86.                         var child = create_MenuItem(idStripped, id, "true", "open_website(event, '" + id + "_url');");
  87.                     }
  88.                     if (document.getElementById(idStripped) == null) {
  89.                         menupopup.insertBefore(child, menupopup.lastChild);
  90.                     }
  91.                 }
  92.             }
  93.             
  94.             if (googlebuttonsPrefs.getBoolPref('dropdown') == true) {
  95.                 if (document.getElementById('googlebuttonsOrientation') != null && document.getElementById('googlebuttonsdropdownmenu') != null)
  96.                 {
  97.                     document.getElementById('googlebuttonsOrientation').hidden = true;
  98.                     document.getElementById('googlebuttonsdropdownmenu').hidden = false;
  99.                 }
  100.             } else {
  101.                 if (document.getElementById('googlebuttonsOrientation') != null && document.getElementById('googlebuttonsdropdownmenu') != null)
  102.                 {
  103.                     document.getElementById('googlebuttonsOrientation').hidden = false;
  104.                     document.getElementById('googlebuttonsdropdownmenu').hidden = true;
  105.                 }
  106.             }
  107.             
  108.             for(var i=0;i<soerenrinneNumberOfServices;i++) {
  109.                 var id = soerenrinneServicesArray[i];
  110.                 var idStripped = id.replace(/\ /gi, "").toLowerCase();
  111.                 try {
  112.                     if (googlebuttonsPrefs.getBoolPref(id) == true)
  113.                     {
  114.                         if (document.getElementById(idStripped) != null)
  115.                             document.getElementById(idStripped).hidden = false;
  116.                         if (document.getElementById(idStripped+'1') != null)
  117.                             document.getElementById(idStripped+'1').hidden = false;
  118.                     }
  119.                     else
  120.                     {
  121.                         if (document.getElementById(idStripped) != null)
  122.                             document.getElementById(idStripped).hidden = true;
  123.                         if (document.getElementById(idStripped+'1') != null)
  124.                             document.getElementById(idStripped+'1').hidden = true;
  125.                     }
  126.                 } catch (e) {    
  127.                     alert("Error on inner switching in renew:\n" + e.name + ", " + e.message + "\nid: " + id + "\nidStripped: " + idStripped);
  128.                 }
  129.             }
  130.             
  131.             if (googlebuttonsPrefs.getBoolPref('settings') == true)
  132.             {
  133.                 if (document.getElementById('settings1') != null)
  134.                     document.getElementById('settings1').hidden = false;
  135.                 if (document.getElementById('settings') != null)
  136.                     document.getElementById('settings').hidden = false;
  137.             }
  138.             else
  139.             {
  140.                 if (document.getElementById('settings1') != null)
  141.                     document.getElementById('settings1').hidden = true;
  142.                 if (document.getElementById('settings') != null)
  143.                     document.getElementById('settings').hidden = true;
  144.             }
  145.             
  146.             if (googlebuttonsPrefs.getBoolPref('orient_horizontally') == true)
  147.             {
  148.                 if (document.getElementById('googlebuttonsOrientation') != null)
  149.                     document.getElementById('googlebuttonsOrientation').orient = "horizontal";
  150.             }
  151.             else
  152.             {
  153.                 if (document.getElementById('googlebuttonsOrientation') != null)
  154.                     document.getElementById('googlebuttonsOrientation').orient = "vertical";
  155.             }
  156.  
  157.         } catch (e) {
  158.                 alert("Error on switching in renew:\n" + e.name + ", " + e.message);
  159.         }
  160.     }
  161.     
  162.     pub.GoogleButtons_onLoad = function() {
  163.         // automatisch die toolbar hinzufⁿgen, wenn sie nicht schon da ist. Aber nur einmal!
  164.         if (googlebuttonsPrefs.getBoolPref('firstLoad')) {
  165.             try {
  166.                 var firefoxnav = document.getElementById("nav-bar");
  167.                 var curSet = firefoxnav.currentSet;
  168.                 if (curSet.indexOf("GoogleShortcuts_button_set") == -1)
  169.                 {
  170.                 var set;
  171.                 // Place the button before the urlbar
  172.                 if (curSet.indexOf("urlbar-container") != -1)
  173.                     set = curSet.replace(/urlbar-container/, "GoogleShortcuts_button_set,urlbar-container");
  174.                 else  // at the end
  175.                     set = curSet + ",GoogleShortcuts_button_set";
  176.                 firefoxnav.setAttribute("currentset", set);
  177.                 firefoxnav.currentSet = set;
  178.                 document.persist("nav-bar", "currentset");
  179.                 // If you don't do the following call, funny things happen
  180.                 try {
  181.                     BrowserToolboxCustomizeDone(true);
  182.                 }
  183.                     catch (e) { alert("Error on automatic adding (inner try):\n" + e.name + ", " + e.message); }
  184.                 }
  185.                 googlebuttonsPrefs.setBoolPref('firstLoad', false);
  186.             }
  187.             catch(e) { alert("Error on automatic adding:\n" + e.name + ", " + e.message); }
  188.         }
  189.         
  190.         try {
  191.             if (document.getElementById('GIcon') != null) {
  192.                 document.getElementById('GIcon').hidden = true;
  193.             }
  194.             
  195.             // Add toolbar items
  196.             if (document.getElementById("googlebuttonsOrientation") != null) {
  197.                 var box = document.getElementById("googlebuttonsOrientation"); // a <menupopup> element
  198.                 
  199.                 for (var j=0; j<soerenrinneNumberOfServices; j++) {
  200.                     var id = soerenrinneServicesArray[j];
  201.                     var idStripped = id.replace(/\ /g, "").toLowerCase();
  202.                     if (googlebuttonsPrefs.getBoolPref(id) == true) {
  203.                         var child = create_ToolbarButton(idStripped+"1", id, "false", "open_website(event, '" + id + "_url');");
  204.                     } else {
  205.                         var child = create_ToolbarButton(idStripped+"1", id, "true", "open_website(event, '" + id + "_url');");
  206.                     }
  207.                     if (document.getElementById(idStripped+"1") == null) {
  208.                         box.insertBefore(child, box.lastChild);
  209.                     }
  210.                 }
  211.             }
  212.                         
  213.             // Add menu items
  214.             if (document.getElementById("googlebuttons_menupopup") != null) {
  215.                 var menupopup = document.getElementById("googlebuttons_menupopup");
  216.                 
  217.                 for (var j=0; j<soerenrinneNumberOfServices; j++) {
  218.                     var id = soerenrinneServicesArray[j];
  219.                     var idStripped = id.replace(/\ /g, "").toLowerCase();
  220.                     if (googlebuttonsPrefs.getBoolPref(id) == true) {
  221.                         var child = create_MenuItem(idStripped, id, "false", "open_website(event, '" + id + "_url');");
  222.                     } else {
  223.                         var child = create_MenuItem(idStripped, id, "true", "open_website(event, '" + id + "_url');");
  224.                     }
  225.                     if (document.getElementById(idStripped) == null) {
  226.                         menupopup.insertBefore(child, menupopup.lastChild);
  227.                     }
  228.                 }
  229.             }
  230.             // now everything is added, let's see how it should look like
  231.             
  232.             if(googlebuttonsPrefs.getBoolPref('dropdown') == true) {
  233.                 if (document.getElementById('googlebuttonsOrientation') != null && document.getElementById('googlebuttonsdropdownmenu') != null)
  234.                 {
  235.                     document.getElementById('googlebuttonsOrientation').hidden = true;
  236.                     document.getElementById('googlebuttonsdropdownmenu').hidden = false;
  237.                 }
  238.             } else {
  239.                 if (document.getElementById('googlebuttonsOrientation') != null && document.getElementById('googlebuttonsdropdownmenu') != null)
  240.                 {
  241.                     document.getElementById('googlebuttonsOrientation').hidden = false;
  242.                     document.getElementById('googlebuttonsdropdownmenu').hidden = true;
  243.                 }
  244.             }
  245.             
  246.             if (googlebuttonsPrefs.getBoolPref('orient_horizontally') == true)
  247.             {
  248.                 if (document.getElementById('googlebuttonsOrientation') != null)
  249.                     document.getElementById('googlebuttonsOrientation').orient = "horizontal";
  250.             }
  251.             else
  252.             {
  253.                 if (document.getElementById('googlebuttonsOrientation') != null)
  254.                     document.getElementById('googlebuttonsOrientation').orient = "vertical";
  255.             }
  256.         } catch (e) {
  257.                 alert("Error onLoad:\n" + e.name + ", " + e.message);
  258.         }
  259.         
  260.         // generate observer
  261.         myListener = new PrefListener("de.soerenrinne.googlebuttons.",
  262.             function(branch, name)
  263.             {              
  264.                 GoogleButtons_renew();
  265.             });
  266.         myListener.register();
  267.         
  268.     }
  269.     
  270.     // First run function
  271.     pub.GoogleButtons_OpenFirstRunPage = function() {
  272.         var VERSION = "1.8.4";
  273.         var URL = "http://www.soerenrinne.de/GoogleShortcuts/update.html";
  274.  
  275.         var pref = googlebuttonsPrefs;
  276.  
  277.         if (!pref.prefHasUserValue("version")) {
  278.             //no version found
  279.             setTimeout(function() { window.openUILinkIn(URL, "tab"); }, 500);
  280.             pref.setCharPref("version", VERSION);
  281.         }
  282.         
  283.         if (pref.getCharPref("version") != VERSION) {
  284.             //newer version
  285.             setTimeout(function() { window.openUILinkIn(URL, "tab"); }, 500);
  286.             pref.setCharPref("version", VERSION);
  287.         }                
  288.     }
  289.     
  290.     open_website = function(event, url) {
  291.     
  292.         var btnCode = event.button;
  293.         var browser = document.getElementById("content");
  294.         var ctrlKey = event.ctrlKey;
  295.         var prefs_url = googlebuttonsPrefs.getCharPref(url);
  296.         var shiftKey = event.shiftKey;
  297.         var focustab = googlebuttonsPrefs.getBoolPref("focustab");
  298.         var alwaysNewTab = googlebuttonsPrefs.getBoolPref("openintab");
  299.         
  300.         if(btnCode == 0) { // left mouse button
  301.                 if(ctrlKey == true) {
  302.                         if(shiftKey == true) {
  303.                             // new tab without focus
  304.                             browser.addTab(prefs_url);
  305.                         } else {
  306.                             // new tab with focus
  307.                             browser.selectedTab = browser.addTab(prefs_url);
  308.                         }
  309.                 } else if(shiftKey == true) {
  310.                         // open in new window
  311.                     window.open(prefs_url);
  312.                 } else if(alwaysNewTab == true && focustab == false) {
  313.                         // new tab with focus
  314.                         browser.selectedTab = browser.addTab(prefs_url);
  315.                 } else if(alwaysNewTab == true) {
  316.                         // new tab without focus
  317.                         browser.addTab(prefs_url);
  318.                 } else {
  319.                     // open in current tab
  320.                         browser.loadURI(prefs_url);
  321.                     }
  322.             } else if(btnCode == 1) { // middle mouse button
  323.                 if(shiftKey == true) {
  324.                         // new tab without focus
  325.                         browser.addTab(prefs_url);
  326.                     } else if(focustab == true){
  327.                         // new tab without focus
  328.                         browser.addTab(prefs_url);
  329.                     } else {
  330.                 // new tab with focus
  331.                 browser.selectedTab = browser.addTab(prefs_url);
  332.                 }
  333.             }
  334.     }
  335.     
  336.     open_settings = function()
  337.     {
  338.         window.openDialog('chrome://googlebuttons/content/googlebuttonsSettings.xul','Settings of Google Shortcuts','chrome,titlebar,toolbar,centerscreen,alwaysRaised,dialog=no,resizable,autostretch');
  339.     }
  340.     
  341.     create_ToolbarButton = function(aButtonID, aButtonTooltiptext, aButtonHidden, aButtonOnclick) {
  342.         const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  343.           var item = document.createElementNS(XUL_NS, "toolbarbutton"); // create a new XUL toolbarbutton
  344.           item.setAttribute("id", aButtonID);
  345.         item.setAttribute("tooltiptext", aButtonTooltiptext);
  346.         item.setAttribute("hidden", aButtonHidden);
  347.         item.setAttribute("class", "iconbutton");
  348.         item.setAttribute("onclick", aButtonOnclick);
  349.         item.setAttribute("label", aButtonTooltiptext);
  350.         item.setAttribute("image", "chrome://googlebuttons/skin/" + aButtonID.replace("1", "") + ".gif");
  351.           return item;
  352.     }
  353.     
  354.     create_MenuItem = function(aMenuID, aMenuTooltiptext, aMenuHidden, aMenuOnclick) {
  355.         const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  356.           var item = document.createElementNS(XUL_NS, "menuitem"); // create a new XUL menuitem
  357.           item.setAttribute("id", aMenuID);
  358.         item.setAttribute("tooltiptext", aMenuTooltiptext);
  359.         item.setAttribute("hidden", aMenuHidden);
  360.         item.setAttribute("class", "menuitem-iconic");
  361.         item.setAttribute("label", aMenuTooltiptext);
  362.         item.setAttribute("onclick", aMenuOnclick);
  363.         item.setAttribute("image", "chrome://googlebuttons/skin/" + aMenuID + ".gif");
  364.           return item;
  365.     }
  366.     
  367.     return pub;
  368. }();
  369.     
  370. window.addEventListener("load", de.soerenrinne.GoogleButtons.GoogleButtons_onLoad, false);
  371. window.addEventListener("load", de.soerenrinne.GoogleButtons.GoogleButtons_OpenFirstRunPage, false);
  372.